package com.guan.ruiji.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guan.ruiji.dto.DishDto;
import com.guan.ruiji.mapper.DishMapper;
import com.guan.ruiji.pojo.Dish;
import com.guan.ruiji.pojo.DishFlavor;
import com.guan.ruiji.service.DishFlavorService;
import com.guan.ruiji.service.DishService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author guanzhenjie
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private DishFlavorService dishFlavorService;

    @Override
    public void save(DishDto dishDto) {

        super.save(dishDto);

        Long id = dishDto.getId();

        List<DishFlavor> list = dishDto.getFlavors();
        for (int i = 0; i < list.size(); i++) {
            DishFlavor dishFlavor = list.get(i);
            dishFlavor.setDishId(id);
        }

        dishFlavorService.saveBatch(list);
    }
}
